package com.omkarpatil.learningjpa2.mapping.onetoone.fk;

import javax.persistence.EntityManager;

import com.omkarpatil.learningjpa2.util.EntityManagerFactoryFactory;

public class TestDriver {

	public static void main(String[] args) {
		/* Creating a person */
		EntityManager em1 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em1.getTransaction().begin();
		
		System.out.println("##### Adding desk to database ...");
		Desk desk = new Desk("I6PL06");
		em1.persist(desk);
		
		System.out.println("##### Adding Employee to database ...");
		Employee emp1 = new Employee("Omkar Patil", desk);
		em1.persist(emp1);
		
		em1.getTransaction().commit();
		em1.close();
		
		System.out.println("##### Adding another employee to database with already occupied desk...");
		EntityManager em2 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em2.getTransaction().begin();
		
		em2.merge(desk);
		
		try {
			Employee emp2 = new Employee("Somebody Else", desk);
			em2.persist(emp2);
		} catch (Exception e) {
			System.out.println("##### Can't do that, it's one-to-one relationship ...");
		}

		em2.getTransaction().commit();
		em2.close();
	}

}
